<?php
/**
 * index.php     Zhuayi 腾讯微博抽象类
 *
 * @copyright    (C) 2005 - 2010  Zhuayi
 * @licenes      http://www.zhuayi.net
 * @lastmodify   2010-10-27
 * @author       zhuayi
 * @QQ			 2179942
 */

class qq_action extends zhuayi
{

	/* 构造函数 */
	function __construct($oauth_token='')
	{
		parent::__construct();
		
		$this->app = $this->load_class('appqq',true);
		
		if (!empty($oauth_token))
		{
			$oauth_token = strings::json_dearray($oauth_token);
			
			$this->app->oauth_token = $oauth_token['oauth_token'];
			$this->app->oauth_token_secret = $oauth_token['oauth_token_secret'];
		}
		
	}
	

	/* 帐号登录 */
	function login($id)
	{
		$token = $this->app->request_token($this->weburl.'/qq/info/'.$id);
		
		$token = explode('&',$token);
		foreach ($token as $key=>$val)
		{
			$val = explode('=',$val);
			$list[$val[0]] = $val[1];
		}
		/* 记录获取来的token */
		cookie::set_cookie('qq_oauth_token',$list['oauth_token']);
		cookie::set_cookie('qq_oauth_token_secret',$list['oauth_token_secret']);
	
		return $url = 'http://open.t.qq.com/cgi-bin/authorize?oauth_token='.$list['oauth_token'];

		
	}
	
	/* 换取account */
	function info($id)
	{
		$this->app->oauth_token = cookie::ret_cookie('qq_oauth_token');
		$this->app->oauth_token_secret = cookie::ret_cookie('qq_oauth_token_secret');
		
		$token = $this->app->access_token($_GET['oauth_verifier']);
		
		$token = explode('&',$token);
		foreach ($token as $key=>$val)
		{
			$val = explode('=',$val);
			$list[$val[0]] = $val[1];
		}
		
		$this->app->oauth_token = $list['oauth_token'];
		$this->app->oauth_token_secret = $list['oauth_token_secret'];
		
		$reset = $this->app->user_info();
		$reset = $reset['data'];
		
		$return['aid'] = $reset['name'];
		$return['name'] = $reset['nick'];
		$return['address'] = $reset['location'];
		$return['description'] = $reset['introduction'];
		$return['face'] = $reset['head'].'/50';
		$return['domain'] = 'http://t.qq.com/'.$reset['name'];
		$return['sex'] = $reset['sex'];
		$return['followers_count'] = $reset['fansnum'];
		$return['friends_count'] = $reset['idolnum'];
		$return['statuses_count'] = $reset['tweetnum'];
		$return['token'] = strings::json_enarray($list);
		$return['id'] = $id;
		
		account_action::landing($return,'qq');
	}
	
	/* 获取微博列表 */
	function weibo_list()
	{
		
		return  $this->app->weibo_list();
	}
	
	/* 评论列表 */
	function comments_list($array)
	{
		$array['rootid'] = $array['id'];
		$array['flag'] = 1;
		return  $this->app->re_list($array);
	}
	
	/* 评论一条信息 */
	function comment($array)
	{
		$reid = $array['id'];
		$content = $array['content'];
		$ip = $this->load_fun('get_ip');
		
		$reset = $this->app->comment($reid,$content,$ip);
		
		/* 获取评论的那条微博信息 */
		return $this->app->weibo($reset['data']['id']);
	}
	
	
	/* 转发 */
	function weibo_repost($array)
	{
		$reid = $array['id'];
		$content = $array['content'];
		$ip = $this->load_fun('get_ip');
		
		$reset =  $this->app->weibo_repost($reid,$content,$ip);
		
		/* 同时评论 */
		if ($array['is_comment'] == 1)
		{
			$this->app->comment($reid,$content,$ip);
		}
		
		/* 获取转发的那条微博信息 */
		return $this->app->weibo($reset['data']['id']);
		
	}
	
	/* 获取转发数和评论数 */
	function status_counts($ids)
	{
		$ids = explode(',',$ids);
		$ids = array_flip(array_flip($ids));
		if (count($ids)>30)
		{
			for($i=27;$i<=(count($ids)+1);$i++)
			{

				unset($ids[$i]);
			}
		}
		//$ids = $ids[0];
		$ids = implode(',',$ids);
		return  $this->app->re_count($ids);
	}
	
	/* 发布一条微博 */
	function weibo_info($status,$file='')
	{
		if (!empty($file))
		$file = str_replace($this->weburl,ZHUAYI_ROOT,$file);

		$reset =  $this->app->weibo_info($status,array('pic',$file));
		
		
		$reset = $reset['data']['id'];
		
		/* 获取转发的那条微博信息 */
		return $this->app->weibo($reset);
	}
	
	/* 删除一条微博 */
	function delete($id)
	{
		return  $this->app->delete($id);
	}
	
	/* @atme 列表 */
	function atme($array)
	{
		return $this->app->atme($array);
	}
	
	/* 我的收藏 */
	function favorites()
	{
		$reset =  $this->app->favorites();
		return $reset;
	}
	
	/* 添加收藏 */
	function add_favorites($id)
	{
		$reset =  $this->app->add_favorites($id);
		return $reset;
	}
	
	/* 删除收藏 */
	function del_favorites($id)
	{
		$reset =  $this->app->del_favorites($id);
		return $reset;
	}
	
	/* 我的微博 */
	function my_weibo()
	{
		return $this->app->my_weibo();
	}
	
	/* 微博字段转换 */
	function array_key($array)
	{
		$array2['face'] = $array['head'].'/50';
		$array2['nick'] = $array['nick'];
		if (!empty($array['text']))
		{
			$array2['text'] = $array['text'];
		}
		else
		{
			$array['text'] = '转发：';
		}
		$array2['source'] = '来自'.$array['from'];
		
		if (!empty($array['image']))
		{
			$array2['litpic'] = $array['image'][0].'/160';
		}
		$array2['dtime'] = $this->load_fun('dtime',$array['timestamp'],2);
		
		if (!empty($array['source']))
		{
			$array2['zhuanfa'] = $array['source'];
			$array2['zhuanfa']['nick'] = $array['source']['nick'];
			$array2['zhuanfa']['text'] = $array['source']['text'];
			
			if (!empty($array['zhuanfa']['image'][0]))
			{
				$array2['zhuanfa']['litpic'] = $array['zhuanfa']['image'][0].'/160';
			}
		}
		
		return $array2;
	}
	

}

?>